package com.fengye.lock.zk.config;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;

/**
 *
 * @author sky
 * @date 2021年06月11日 1:14 下午
 */
@Configuration
public class ZKConfig {

    @Value("${zk.host:127.0.0.1:2181}")
    private String host;

    @Value("${zk.namespace:lock}")
    private String namespace;
    @Bean
    public CuratorFramework curatorFramework() {
        // 创建 CuratorFramework 实例
        //   1. 采用工厂模式创建
        //   2. 指定了客户端连接到 zookeeper 服务端的策略：重试机制
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(host)
                .namespace(namespace)
                .retryPolicy(new RetryNTimes(5, 1000)).build();

        curatorFramework.start();
        return curatorFramework;
    }
}
